using System.Linq;
using UnityEngine;
using UnityEngine.UI;

namespace CGF
{
    public static class ColorHelper
    {
        /// <summary>
        /// 16进制颜色值转Color
        /// </summary>
        public static Color Color(string colorCode, float alpha = 1)
        {
            Color color;
            if (!colorCode.Contains("#")) colorCode = "#" + colorCode;
            ColorUtility.TryParseHtmlString(colorCode, out color);
            color.a = alpha;
            return color;
        }

        /// <summary>
        /// 设置当前预制体所以节点的颜色值
        /// </summary>
        public static void SetColor(this RectTransform trans, Color color, string[] ignoreNames = null)
        {
            if (!trans) return;
            MaskableGraphic[] nodes = trans.GetComponentsInChildren<MaskableGraphic>();
            for (int i = 0; i < nodes.Length; i++)
            {
                if (ignoreNames != null && ignoreNames.Contains(nodes[i].name)) continue;
                nodes[i].color = color;
            }
        }

    }
}